cc.Class({
    extends: cc.Component,

    properties: {
        
    },
    
    // use this for initialization
    onLoad: function () {
        this.initFlag = false;
        this.speed = 30;
        this.body = this.node.getComponent(cc.RigidBody);
    },

    init:function(pos,rotation,pool){
        if(!this.initFlag){
            this.pool = pool;
            this.initFlag = true;
        }
        this.node.scale = cc.v2(1,1);
        this.flag = true;
        let angle = (180-rotation)*Math.PI/180;
        let ranAngle = angle+Math.PI/4*cc.randomMinus1To1(); 
        let targetPos = cc.v2(pos.x-600*Math.sin(ranAngle),pos.y-600*Math.cos(ranAngle));
        this.node.position = this.node.parent.convertToNodeSpaceAR(targetPos);
        let rolePos = this.node.parent.convertToNodeSpaceAR(pos);
        this.node.rotation = rotation;
        this.body.linearVelocity = cc.v2(this.speed*Math.sin(angle),this.speed*Math.cos(angle));
    },

    disappear:function(){
        this.node.stopAllActions();
        this.pool.put(this.node);
    },

    onBeginContact:function (contact, selfCollider, otherCollider) {
        if(this.flag){
            let role = otherCollider.getComponent('role');
            if(role){
                role.speed = 0;
                role.speedTxt.string = '0';
                role.showTip('注意躲开毒云');
                this.flag = false;
                this.node.runAction(cc.sequence(cc.scaleTo(1,0),cc.callFunc(function(){
                    this.pool.put(this.node);
                },this)));
            }else{
                this.disappear();
            }
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
